package com.xiaode.controller.app.file;

import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.ext.cors.EnableCORS;
import com.jfinal.kit.Ret;
import com.xiaode.common.anotation.BooleanV;
import com.xiaode.common.anotation.JSONArrayString;
import com.xiaode.util.apidoc.annotation.Api;
import com.xiaode.util.apidoc.annotation.ApiOper;
import com.xiaode.util.apidoc.annotation.ApiPara;

import javax.validation.constraints.Size;

/**
 * 文件资源相关服务
 */
@Path("/file")
@Api("文件资源相关服务")
@EnableCORS
public class FileManageController extends Controller {
    @Inject
    private FileManageService service;

    /**
     * 获取聊天文件前端上传参数
     * 1.图片
     * 2.语音
     * 3.其他文件
     */
    @ApiOper("获取聊天文件前端上传参数")
    public void getUploadMsgFileParams(
            @JSONArrayString @ApiPara(value = "fileInfo", notes = "文件类型[{upType:,md5:}]") String fileInfo,
            @Size @ApiPara(value = "id", notes = "目标id") Integer id,
            @Size @ApiPara(value = "type", notes = "目标类型 1.学生 2.教师 3.群聊") Integer type) throws Exception {
//        Object user = getAttr("user");
        Integer accountId = getAttr("accountId");
    	Integer userType = getAttr("userType");
        Ret ret = service.getUploadMsgFileParams(accountId, userType, fileInfo, id, type);
        renderJson(ret);
    }

    @ApiOper("xiaode获取文件前端上传参数")
    public void getUploadFileParams(@JSONArrayString @ApiPara(value = "fileInfo", notes = "文件类型[{upType:,md5:}]") String fileInfo)throws Exception{
        Integer accountIdMy = getAttr("accountId");
        Ret ret = service.getUploadFileParams(accountIdMy, fileInfo);
        renderJson(ret);
    }

    @ApiOper("上传文件确认")
    public void confirmUploadFile(@JSONArrayString @ApiPara(value = "confirmFile", notes = "文件类型[{key:XXX,md5:XXX,type:XXX}]") String confirmFile) throws Exception {
        //1聊天 2.xiaode
        Ret ret = service.confirmUploadFile(confirmFile,1);
        renderJson(ret);
    }

    @ApiOper("xiaode上传文件确认")
    public void xiaodeConfirmUploadFile(@JSONArrayString @ApiPara(value = "confirmFile", notes = "文件类型[{key:XXX,md5:XXX,type:XXX}]") String confirmFile) throws Exception {
        Ret ret = service.confirmUploadFile(confirmFile,2);
        renderJson(ret);
    }

    /**
     * 文件获取
     * isThumbnail参数仅图片可用
     */
    @ApiOper("文件获取")
    public void getDownloadUrl(
            @JSONArrayString @ApiPara(value = "fileNames", notes = "文件名称[fileName]") String fileNames,
            @BooleanV @ApiPara(value = "isThumbnail", notes = "是否缩略图") Boolean isThumbnail) throws Exception {
        Ret ret = service.getDownloadUrl(fileNames, isThumbnail,1);
        renderJson(ret);
    }

    @ApiOper("xiaode文件获取")
    public void xiaodeGetDownloadUrl(
            @JSONArrayString @ApiPara(value = "fileNames", notes = "文件名称[fileName]") String fileNames,
            @BooleanV @ApiPara(value = "isThumbnail", notes = "是否缩略图") Boolean isThumbnail) throws Exception {
        Ret ret = service.getDownloadUrl(fileNames, isThumbnail,2);
        renderJson(ret);
    }

}
